package com.woniuedu.educenter.controller;


import com.woniuedu.commonutils.JwtTokenUtil;
import com.woniuedu.commonutils.R;
import com.woniuedu.educenter.entity.UcenterMember;
import com.woniuedu.educenter.entity.vo.RegisterVo;
import com.woniuedu.educenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Description:会员表
 * @author: ykq11  <br/>
 * Date: 2022/4/26:14:26  <br/>
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

/**
 * Description:登录
 * @author: ykq11  <br/>
 * Date: 2022/4/26:14:34  <br/>
 */
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
        //返回token值
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }


/**
 * Description:注册
 * @author: ykq11  <br/>
 * Date: 2022/4/26:14:34  <br/>
 */
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * Description:根据token获取用户id
     * @author: ykq11  <br/>
     * Date: 2022/4/26:14:35  <br/>
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法，根据request对象获取头信息
        String memberId = JwtTokenUtil.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    @GetMapping("getUcenter/{memberId}")
    public UcenterMember getUcenterById(@PathVariable("memberId") String memberId){
        UcenterMember member = memberService.getById(memberId);
        return member;
    }

   /**
    * Description: 查询某一天注册人数
    * @author: ykq11  <br/>
    * Date: 2022/4/26:14:35  <br/>
    */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }
}

